//
//  HRDynamicInfoHeadView.m
//  Hear
//
//  Created by mac on 2017/4/11.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDynamicInfoHeadView.h"
#import "YYControl.h"
#import "HRDunamicLayout.h"
#import "YYPhotoGroupView.h"
#import "HRCoverTimeModel.h"
#import "HRCoverImageModel.h"
#import "NSMutableAttributedString+GDAttString.h"
#import "HRUMShareTool.h"

@implementation HRDynamicToolView



-(void)setDynamicModel:(HRDynamicModel *)dynamicModel{
    
    _dynamicModel = dynamicModel;
    
    if ([dynamicModel.IsLike intValue]){
        [self.likeButton setImage:[UIImage imageNamed:@"点赞_已点赞"] forState:UIControlStateNormal];
        _likeButton.selected = YES;
    }else{
        [self.likeButton setImage:[UIImage imageNamed:@"点赞_未点赞"] forState:UIControlStateNormal];
        _likeButton.selected = NO;
    }
    [self.likeButton setTitle:[NSString stringWithFormat:@"  %ld",(long)dynamicModel.LikeCount] forState:0];
    
    [self.messageButton setTitle:[NSString stringWithFormat:@"  %ld",(long)dynamicModel.CommentCount] forState:0];
    
    
    

}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {

        
        
        self.messageButton = [[UIButton alloc] initWithFrame:CGRectMake(kFullWidth-60, 0, 50, 20)];
        [self.messageButton setTitleColor:CustomColor(190, 190, 190, 1) forState:UIControlStateNormal];
        self.messageButton.titleLabel.font = CustomUIFont(12);
        [self.messageButton setImage:[UIImage imageNamed:@"评论"] forState:UIControlStateNormal];
        [self addSubview:self.messageButton];
        self.messageButton.userInteractionEnabled = NO;
        
        
        self.likeButton = [[EmitterBtn alloc] initWithFrame:CGRectMake(self.messageButton.left-self.messageButton.width-5, 0, 50, 20)];
        [self.likeButton setTitleColor:CustomColor(190, 190, 190, 1) forState:UIControlStateNormal];
        self.likeButton.titleLabel.font = CustomUIFont(12);
        [self.likeButton setImage:[UIImage imageNamed:@"点赞_未点赞"] forState:UIControlStateNormal];
        [self.likeButton addTarget:self action:@selector(likeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.likeButton];
        
//        self.shareButton = [[UIButton alloc] initWithFrame:CGRectMake(self.messageButton.left-self.likeButton.width-5, 0, 80, 20)];
//        self.shareButton.titleLabel.font = CustomUIFont(12);
//        [self.shareButton setImage:[UIImage imageNamed:@"转发按钮"] forState:UIControlStateNormal];
//        [self.shareButton addTarget:self action:@selector(shareButtonClick) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:self.shareButton];
        
    }
    return self;
}

//- (void)shareButtonClick{
//    NSString *contentStr = @"";
//    if (self.dynamicModel.MomentNotes.length){
//        contentStr = self.dynamicModel.MomentNotes;
//    }else{
//        contentStr = @"我在Hear留下的瞬间";
//    }
//    
//    
//    [HRUMShareTool uMShareToolTitle:contentStr andContentStr:contentStr andImage:[UIImage imageNamed:@"hearicon"] andShareUrl:[NSString stringWithFormat:@"http://www.dolphinvoice.com:82/M/MomentShare/%@",self.dynamicModel.LocalId]];
//}


- (void)likeButtonClick{
    if (!_likeButton.selected){
        [self.likeButton starAmimation];
        [self.likeButton setTitle:[NSString stringWithFormat:@"  %d",[self.likeButton.titleLabel.text intValue]+1] forState:0];
        [self.likeButton setImage:[UIImage imageNamed:@"点赞_已点赞"] forState:UIControlStateNormal];
        _likeButton.selected = YES;
        
        if (self.likeButtonClickBlock){
            self.likeButtonClickBlock(YES);
        }
    }
}

@end


@implementation VBHeadContentView

-(void)setLayout:(HRDunamicLayout *)layout{
    _layout = layout;
    WeakSelf
    _contView.frame = CGRectMake(0, 0, kFullWidth, layout.cellHeight);
    
    _iconImageView.frame = layout.iconImageViewRect;
    
    self.nickNameLabel.frame = CGRectMake(_iconImageView.right+10, _iconImageView.top, 200, _iconImageView.height);
    
    
    _contentLabel.frame = layout.contentRect;
    
    _timeLabel.frame = layout.timeRect;
    
    self.soundButton.frame = layout.soundButtonRect;

    
    _toolView.frame = layout.toolViewRect;
    
    self.nickNameLabel.text = layout.dynamicModel.NickName;

    _toolView.dynamicModel = layout.dynamicModel;
    
    self.contentLabel.attributedText = [NSMutableAttributedString attStringFromTextForChatting:layout.dynamicModel.MomentNotes textFont:CustomUIFont(14)];

    
    
    
    self.timeLabel.text = [HRCoverTimeModel covetTime:layout.dynamicModel.CreateTime];
    
    self.soundButton.dynamicModel = layout.dynamicModel;

    
    if ([layout.dynamicModel.UserLocalId isEqualToString:@"admin"]){
        weakSelf.iconImageView.image = [UIImage imageNamed:@"小助手"];
    }else{
//        ((YYControl *)self.iconImageView).image = [HRCoverImageModel OriginImage:[UIImage imageNamed:layout.dynamicModel.UserPictureName] scaleToSize:CGSizeMake(50, 50)];
        
        [HRCoverImageModel OriginImage:[UIImage imageNamed:layout.dynamicModel.UserPictureName] scaleToSize:CGSizeMake(50, 50) callBackOriginImage:^(UIImage *originImage) {
            ((YYControl *)weakSelf.iconImageView).image = originImage;
        }];
    }

    
    
    
    self.iconImageView.backgroundColor = [UIColor colorWithHexString:layout.dynamicModel.UserPictureBackgroundColor];
    
    [self setImageView];
    
    
    
}

- (void)setImageView{
//    NSArray *pics = _layout.dynamicModel.pics;
//    
//    int picsCount = (int)pics.count;
//    
//    for (int i = 0; i < 6; i ++) {
//        UIView *imageView = _picViews[i];
//        if (i >= picsCount) {
//            [imageView.layer cancelCurrentImageRequest];
//            imageView.hidden = YES;
//        } else{
//            imageView.frame = CGRectMake(_layout.picPading, _contentLabel.top+_contentLabel.height+_layout.picPading+i*(_layout.picSize.height+_layout.picPading), _layout.picSize.width, _layout.picSize.height);
//            imageView.hidden = NO;
//            [imageView.layer removeAnimationForKey:@"contents"];
//            
//            //[imageView.layer setImageURL:[NSURL URLWithString:_layout.dynamicModel.pics[i]]];
//            
//            
//            @weakify(imageView);
//            [imageView.layer setImageWithURL:_layout.dynamicModel.pics[i]
//                                 placeholder:[UIImage imageNamed:@"默认加载图"]
//                                     options:YYWebImageOptionAvoidSetImage
//                                  completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error){                             @strongify(imageView);
//                                      if (!imageView) return;
//                                      if (image && stage == YYWebImageStageFinished) {
//                                          int width = image.size.width;
//                                          int height = image.size.height;
//                                          CGFloat scale = (height / width) / (imageView.height / imageView.width);
//                                          if (scale < 0.99 || isnan(scale)) { // 宽图把左右两边裁掉
//                                              imageView.contentMode = UIViewContentModeScaleAspectFill;
//                                              imageView.layer.contentsRect = CGRectMake(0, 0, 1, 1);
//                                          } else { // 高图只保留顶部
//                                              imageView.contentMode = UIViewContentModeScaleAspectFill;
//                                              imageView.layer.contentsRect = CGRectMake(0, 0, 1, (float)width / height);
//                                          }
//                                          ((YYControl *)imageView).image = image;
//                                          if (from != YYWebImageFromMemoryCacheFast) {
//                                              CATransition *transition = [CATransition animation];
//                                              transition.duration = 0.15;
//                                              transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
//                                              transition.type = kCATransitionFade;
//                                              [imageView.layer addAnimation:transition forKey:@"contents"];
//                                          }
//                                      }
//                                  }];
//            
//        }
//    }
}



-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        WeakSelf
        
        self.backgroundColor = [UIColor clearColor];
        self.exclusiveTouch = YES;
        _contView = [UIView new];
        _contView.width = kScreenWidth;
        _contView.height = 1;
        _contView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_contView];
        
        self.lockImageView = [UIImageView new];
        self.lockImageView.image = [UIImage imageNamed:@"锁-"];
        [_contView addSubview:self.lockImageView];
        
        self.iconImageView = [UIImageView new];
        [_contView addSubview:self.iconImageView];
        self.iconImageView.layer.cornerRadius = 25;
        self.iconImageView.contentMode = UIViewContentModeScaleToFill;
        
        self.nickNameLabel = [UILabel new];
        self.nickNameLabel.textColor = [UIColor blackColor];
        self.nickNameLabel.font = CustomUIFont(14);
        [_contView addSubview:self.nickNameLabel];
        
        
        _timeLabel = [UILabel new];
        
        _timeLabel.font = CustomUIFont(13);
        _timeLabel.textColor = CustomColor(180, 180, 180, 1);
        [_contView addSubview:_timeLabel];
        _timeLabel.textAlignment = 2;
        
        _contentLabel = [YYLabel new];
        _contentLabel.numberOfLines = 0;
        _contentLabel.textColor = TEXTCOLOR;
        [_contView addSubview:_contentLabel];
        _contentLabel.font = CustomUIFont(14);

        _soundButton = [HRSoundButton buttonWithType:0];
        [_contView addSubview:_soundButton];
        _soundButton.playMedioBlock = ^(HRDynamicModel *dynamicModel){
            if (weakSelf.playMedioBlock){
                weakSelf.playMedioBlock(dynamicModel);
            }
        };
        
        
        
        
        
        NSMutableArray *picViews = [NSMutableArray new];
        for (int i = 0; i < 6; i++) {
            YYControl *imageView = [YYControl new];
            imageView.userInteractionEnabled = YES;
            imageView.size = CGSizeMake(100, 100);
            imageView.hidden = YES;
            imageView.clipsToBounds = YES;
            imageView.backgroundColor = TEXTCOLOR;
            imageView.exclusiveTouch = YES;
//            imageView.touchBlock = ^(YYControl *view, YYGestureRecognizerState state, NSSet *touches, UIEvent *event) {
//                //                if (![weakSelf.cell.delegate respondsToSelector:@selector(cell:didClickImageAtIndex:)]) return;
//                if (state == YYGestureRecognizerStateEnded) {
//                    [weakSelf clickImageAtIndex:i];
//
//                    
//                    UITouch *touch = touches.anyObject;
//                    CGPoint p = [touch locationInView:view];
//                    if (CGRectContainsPoint(view.bounds, p)) {
//                        //[weak_self.cell.delegate cell:weak_self.cell didClickImageAtIndex:i];
////                        [weakSelf clickImageAtIndex:i];
////                        [weakSelf clickImageAtIndex:i];
//
//                    }
//                }
//            };
            UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
                [weakSelf clickImageAtIndex:i];
            }];
            [imageView addGestureRecognizer:imageTap];
            [picViews addObject:imageView];
            
            
            [_contView addSubview:imageView];
        }
        
        _picViews = picViews;
        
        
       
        _toolView = [HRDynamicToolView new];
        
        [_contView addSubview:_toolView];
        _toolView.likeButtonClickBlock = ^(BOOL isLike){
            if (weakSelf.likeButtonClickBlock){
                weakSelf.likeButtonClickBlock(isLike);
            }
        };
    }
    return self;
}


- (void)clickImageAtIndex:(NSUInteger)index{
    UIView *fromView = nil;
    NSMutableArray *items = [NSMutableArray new];
    NSArray *pics =_layout.dynamicModel.pics;
    
    for (NSUInteger i = 0, max = pics.count; i < max; i++) {
        UIView *imgView = self.picViews[i];
        YYPhotoGroupItem *item = [YYPhotoGroupItem new];
        item.thumbView = imgView;
        item.largeImageURL = [NSURL URLWithString:pics[i]];
        item.largeImageSize = CGSizeMake(imgView.width, imgView.height);
        [items addObject:item];
        if (i == index) {
            fromView = imgView;
        }
    }
    
    YYPhotoGroupView *v = [[YYPhotoGroupView alloc] initWithGroupItems:items];
    [v presentFromImageView:fromView toContainer:VIEWCCONTROLER.view animated:YES completion:nil];
}

@end



@implementation HRDynamicInfoHeadView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        WeakSelf
        self.contenView = [VBHeadContentView new];
        [self addSubview:self.contenView];
        self.contenView.playMedioBlock = ^(HRDynamicModel *dynamicModel){
            if (weakSelf.playMedioBlock){
                weakSelf.playMedioBlock(dynamicModel);
            }
        };
        
        self.contenView.likeButtonClickBlock = ^(BOOL isLike){
            if (weakSelf.likeButtonClickBlock){
                weakSelf.likeButtonClickBlock(isLike);
            }
        };
    }
    return self;
}

-(void)setLayout:(HRDunamicLayout *)layout{
    _layout = layout;
    self.contenView.frame = CGRectMake(0, 0, kFullWidth, layout.cellHeight);
    self.contenView.layout = layout;
}


@end
